package cn.dakaqi.services.impl;

import cn.dakaqi.dao.PassportAppointmentDateDao;
import cn.dakaqi.entities.PassportAppointmentDate;
import cn.dakaqi.services.PassportAppointmentDateService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: PassportAppointmentDateServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/10/24 11:31
 * @version: 1.0.0
 */
@Transactional
@Component(value = "passportAppointmentDateService")
public class PassportAppointmentDateServiceImpl implements PassportAppointmentDateService
{
    @Autowired
    PassportAppointmentDateDao passportAppointmentDateDao;

    @Override
    public void save(Long baseAddressId,String day, String time,int num) throws ServiceRuntimeException
    {
        String days[] = day.split(";");
        String times[] = time.split(";");
        for(String d:days)
        {
            for(String t:times)
            {
                PassportAppointmentDate pad = new PassportAppointmentDate(baseAddressId, d, t,num);
                this.save(pad);
            }
        }
    }

    @Override
    public void save(PassportAppointmentDate passportAppointmentDate) throws ServiceRuntimeException
    {
        this.passportAppointmentDateDao.save(passportAppointmentDate);
    }

    @Override
    public void update(PassportAppointmentDate passportAppointmentDate) throws ServiceRuntimeException
    {
        this.passportAppointmentDateDao.save(passportAppointmentDate);
    }

    @Override
    public void updateNum(PassportAppointmentDate pad) throws ServiceRuntimeException
    {
        try
        {
            int num = pad.getNum()-1;
            if(num <=0)
            {
                pad.setNum(0);
                pad.setStatus(PassportAppointmentDate.STATUS_NO);
            }
            else
            {
                pad.setNum(num);
            }
            this.passportAppointmentDateDao.save(pad);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public List<PassportAppointmentDate> findByDay(Long baseAddressId,String day) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("baseAddressId", new SearchFilter("baseAddressId", SearchFilter.Operator.EQ, baseAddressId));
        filters.put("day", new SearchFilter("day", SearchFilter.Operator.EQ, day));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, PassportAppointmentDate.STATUS_YES));
        Specification<PassportAppointmentDate> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportAppointmentDate.class);
        List<PassportAppointmentDate> list = this.passportAppointmentDateDao.findAll(spec);
        return list;
    }

    @Override
    public List<PassportAppointmentDate> findByBaseaddressId(Long baseAddressId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        Sort sort = new Sort(Sort.Direction.ASC, "day","times");
        PageRequest pageRequest = new PageRequest(0, 500, sort);
        filters.put("baseAddressId", new SearchFilter("baseAddressId", SearchFilter.Operator.EQ, baseAddressId));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, PassportAppointmentDate.STATUS_YES));
        Specification<PassportAppointmentDate> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportAppointmentDate.class);
        Page<PassportAppointmentDate> data = this.passportAppointmentDateDao.findAll(spec,pageRequest);
        if(null != data && data.getContent() != null && data.getContent().size()>0)
            return data.getContent();

        return null;
    }

    @Override
    public List<PassportAppointmentDate> findAfterByBaseaddressId(Long baseAddressId, String startDay,String endDay) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        Sort sort = new Sort(Sort.Direction.ASC, "day","times");
        PageRequest pageRequest = new PageRequest(0, 500, sort);
        filters.put("baseAddressId", new SearchFilter("baseAddressId", SearchFilter.Operator.EQ, baseAddressId));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, PassportAppointmentDate.STATUS_YES));
        filters.put("day",new SearchFilter("day", SearchFilter.Operator.GTE, startDay));
        //filters.put("day",new SearchFilter("day", SearchFilter.Operator.LTE, endDay));
        Specification<PassportAppointmentDate> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportAppointmentDate.class);
        Page<PassportAppointmentDate> data = this.passportAppointmentDateDao.findAll(spec,pageRequest);
        if(null != data && data.getContent() != null && data.getContent().size()>0)
            return data.getContent();

        return null;
    }

    @Override
    public Page<PassportAppointmentDate> findAll(Long baseAddressId, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "day");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("baseAddressId", new SearchFilter("baseAddressId", SearchFilter.Operator.EQ, baseAddressId));
        Specification<PassportAppointmentDate> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportAppointmentDate.class);
        return this.passportAppointmentDateDao.findAll(spec,pageRequest);
    }

    @Override
    public PassportAppointmentDate findByDayAndTime(Long baseAddressId, String day, String time) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("baseAddressId", new SearchFilter("baseAddressId", SearchFilter.Operator.EQ, baseAddressId));
        filters.put("day", new SearchFilter("day", SearchFilter.Operator.EQ, day));
        filters.put("times", new SearchFilter("times", SearchFilter.Operator.EQ, time));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, PassportAppointmentDate.STATUS_YES));
        filters.put("num", new SearchFilter("num", SearchFilter.Operator.GT, 0));
        Specification<PassportAppointmentDate> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportAppointmentDate.class);
        List<PassportAppointmentDate> list = this.passportAppointmentDateDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }
}
